{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Opening & Closing a File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:37:28.052812Z",
     "start_time": "2018-06-06T04:37:28.033261Z"
    }
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'test.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-9426bf859622>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'test.txt'"
     ]
    }
   ],
   "source": [
    "f = open('test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:25:29.032904Z",
     "start_time": "2018-06-06T04:25:29.030397Z"
    }
   },
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## File Object Properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:29:39.179147Z",
     "start_time": "2018-06-06T04:29:39.171126Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File Name:  test.txt\n",
      "File Mode:  w\n",
      "Is file readable:  False\n",
      "Is file Writable:  True\n",
      "Is file closed:  False\n",
      "Is file closed:  True\n"
     ]
    }
   ],
   "source": [
    "f = open(\"test.txt\", 'w')\n",
    "\n",
    "print('File Name: ', f.name)\n",
    "\n",
    "print('File Mode: ', f.mode)\n",
    "\n",
    "print('Is file readable: ', f.readable())\n",
    "\n",
    "print('Is file Writable: ', f.writable())\n",
    "\n",
    "print('Is file closed: ', f.closed)\n",
    "\n",
    "f.close()\n",
    "\n",
    "print('Is file closed: ', f.closed)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read Data from text files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:39:26.673103Z",
     "start_time": "2018-06-06T04:39:26.668088Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "sdf\n",
      "sdf\n",
      "sd\n",
      "fsd\n",
      "f\n",
      "sdf\n",
      "sf\n",
      "sdf\n",
      "sdf\n",
      "sd\n",
      "f\n"
     ]
    }
   ],
   "source": [
    "# read() -> read all the data\n",
    "\n",
    "\n",
    "f = open('test.txt','r')\n",
    "\n",
    "data = f.read()\n",
    "\n",
    "print(data)\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:41:27.625547Z",
     "start_time": "2018-06-06T04:41:27.619031Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "sdf\n",
      "sdf\n",
      "s\n"
     ]
    }
   ],
   "source": [
    "# read(n) -> read first n characters\n",
    "\n",
    "f = open('test.txt','r')\n",
    "\n",
    "data = f.read(10)\n",
    "\n",
    "print(data)\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:44:46.691521Z",
     "start_time": "2018-06-06T04:44:46.679490Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "sdf\n",
      "\n",
      "sdf\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# readline() -> read only first line\n",
    "\n",
    "\n",
    "f = open('test.txt','r')\n",
    "\n",
    "line1 = f.readline()\n",
    "\n",
    "print(line1)\n",
    "\n",
    "line2 = f.readline()\n",
    "\n",
    "print(line2)\n",
    "\n",
    "line3 = f.readline()\n",
    "\n",
    "print(line3)\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:47:45.754270Z",
     "start_time": "2018-06-06T04:47:45.749254Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "\n",
      "\n",
      "sdf\n",
      "\n",
      "sdf\n",
      "\n",
      "sd\n",
      "\n",
      "fsd\n",
      "\n",
      "f\n",
      "\n",
      "sdf\n",
      "\n",
      "sf\n",
      "\n",
      "sdf\n",
      "\n",
      "sdf\n",
      "\n",
      "sd\n",
      "\n",
      "f\n"
     ]
    }
   ],
   "source": [
    "# readlines() -> read all the lines into list\n",
    "\n",
    "\n",
    "f = open('test.txt', 'r')\n",
    "\n",
    "lines = f.readlines()\n",
    "\n",
    "print(type(lines))\n",
    "\n",
    "for l in lines:\n",
    "    print(l)\n",
    "    \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write Data to text files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:51:32.495535Z",
     "start_time": "2018-06-06T04:51:32.463449Z"
    }
   },
   "outputs": [],
   "source": [
    "f = open('test.txt', 'a')\n",
    "\n",
    "f.write(\"abc\")\n",
    "\n",
    "f.write(\"xyz\\n\")\n",
    "\n",
    "f.write(\"lmn\")\n",
    "\n",
    "f.close()\n",
    "\n",
    "# f = open('test.txt', 'a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:57:51.097713Z",
     "start_time": "2018-06-06T04:57:51.065125Z"
    }
   },
   "outputs": [],
   "source": [
    "# writeline takes a sequence\n",
    "\n",
    "f = open('test.txt', 'w')\n",
    "\n",
    "lines = ['line-1', 'line-2', 'line-3']\n",
    "\n",
    "f.writelines(lines)\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## r+ Mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:58:29.790108Z",
     "start_time": "2018-06-06T04:58:29.779079Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line-1line-2line-3\n"
     ]
    }
   ],
   "source": [
    "f = open('test.txt','r+')\n",
    "\n",
    "data = f.read()\n",
    "\n",
    "print(data)\n",
    "\n",
    "f.write('asdfghjkl')\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T04:58:33.086207Z",
     "start_time": "2018-06-06T04:58:33.080191Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line-1line-2line-3asdfghjkl\n"
     ]
    }
   ],
   "source": [
    "f = open('test.txt')\n",
    "\n",
    "data = f.read()\n",
    "\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:06:20.634323Z",
     "start_time": "2018-06-06T05:06:20.616778Z"
    }
   },
   "outputs": [],
   "source": [
    "f = open('test.txt','r+')\n",
    "\n",
    "f.write('\\nqwertyuiop\\n')\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 'with' Statement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:11:47.426468Z",
     "start_time": "2018-06-06T05:11:47.407402Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Is file closed:  False\n",
      "Is file closed:  True\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt', 'a') as f:\n",
    "    f.write('\\nabc\\n')\n",
    "    f.write('xyz\\n')\n",
    "    print('Is file closed: ', f.closed)\n",
    "print('Is file closed: ', f.closed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## seek() and tell()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:15:53.119649Z",
     "start_time": "2018-06-06T05:15:53.102596Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "abc\n",
      "\n",
      "5\n",
      "def\n",
      "g\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "f = open('test.txt', 'r')\n",
    "\n",
    "print(f.tell()) # Current Position of the cursor. Print Index\n",
    "\n",
    "print(f.read(4))\n",
    "\n",
    "print(f.tell()) # Current Position of the cursor. Print Index\n",
    "\n",
    "print(f.read(5))\n",
    "\n",
    "print(f.tell()) # Current Position of the cursor. Print Index\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:20:13.017479Z",
     "start_time": "2018-06-06T05:20:13.002439Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The current file pointer position:  0\n",
      "The current file pointer position:  28\n",
      "The current file pointer position:  2\n",
      "The current file pointer position:  10\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt', 'r+') as f:\n",
    "    print('The current file pointer position: ', f.tell())\n",
    "    \n",
    "    text = f.read()\n",
    "    \n",
    "    print('The current file pointer position: ', f.tell())\n",
    "    \n",
    "    f.seek(2)\n",
    "    \n",
    "    print('The current file pointer position: ', f.tell())\n",
    "    \n",
    "    f.write('HELL NO!')\n",
    "    \n",
    "    print('The current file pointer position: ', f.tell())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Renaming & Deleting files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:22:56.579668Z",
     "start_time": "2018-06-06T05:22:56.573653Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.rename('test.txt', 'test-1.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:23:04.326993Z",
     "start_time": "2018-06-06T05:23:04.323484Z"
    }
   },
   "outputs": [],
   "source": [
    "os.remove('test-1.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Splitting Lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T05:26:09.278300Z",
     "start_time": "2018-06-06T05:26:09.270279Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['dsas', 'das', 'dsd', 'fsdf']\n",
      "['sdf', 'sdf', 'df', 'dfg']\n",
      "[]\n",
      "['dfgdf', 'dfg', 'g', 'gh']\n",
      "[]\n",
      "['dg']\n",
      "['gh']\n",
      "['fghfghfgh', 'fgh', 'fgh']\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "    for l in lines:\n",
    "        words = l.split()\n",
    "        print(words)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Assignments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Q.1- Write a Python program to read last n lines of a file\n",
    " \n",
    " Q.3- Write a Python program to copy the contents of a file to another file \n",
    " \n",
    " Q.4- Write a Python program to combine each line from first file with the corresponding line in second file. \n",
    "  \n",
    " Q.5- Write a Python program to write 10 random numbers into a file. Read the file and then sort the numbers and then store it to another file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T02:00:49.708293Z",
     "start_time": "2018-06-06T02:00:49.688347Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('test-1.txt', 'r') as f1:\n",
    "    with open('test-2.txt', 'w') as f2:\n",
    "        for line in f1:\n",
    "            f2.write(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-06-06T02:01:59.482337Z",
     "start_time": "2018-06-06T02:01:59.470459Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('test-1.txt', 'r') as f1, open('test-2.txt', 'w') as f2:\n",
    "    for line in f1:\n",
    "        f2.write(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
